from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_create_script_group_dialog import Ui_Dialog_create_script_group
from database.db import Database

class Create_script_group_signal(QObject):
    save = Signal(str)


class Create_script_group_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Create_script_group_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_create_script_group()
        self.ui.setupUi(self)

        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Create_script_group_signal()
        
        self.ui.pushButton_ok.clicked.connect(self.slot_save)

    def slot_show(self):
        self.ui.lineEdit_name.clear()
        self.show()

    def slot_save(self):
        name = self.ui.lineEdit_name.text()
        if name.isspace() or len(name)==0:
            QMessageBox().information(self,'提示！','分组名称不能为空!',QMessageBox.Ok)
            return
        # 判断分组是否已经存在
        db = Database()
        count = db.select('script_group',['COUNT(*)'],'name=?',params=(name,))[0][0]
        if count > 0:
            QMessageBox().information(self,'提示！','分组已存在!',QMessageBox.Ok)
            db.close_conn()
            return
        else:
            # 存储分组
            db.insert('script_group',['name'],(name,))
            db.close_conn()
            self.signal.save.emit(name)
            self.hide()
        
        
            

